/*
 *
 * Copyright 2011 OpenAjax Alliance
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

Components.utils.import("resource://oaa-cache-inspector/cache-inspector-common.js");

/* ---------------------------------------------------------------- */
/*                  Initialize namespace properties                 */
/* ---------------------------------------------------------------- */

if (typeof OAA_CI == "undefined") {
  var OAA_CI = {};
}

/**
 * @function removeChildNodes
 *
 * @desc removes the child nodes of a DOM object
 *
 * @param {Object} node  - Object DOM node object
 */

OAA_CI.removeChildNodes = function (node) {

  if (node && node.hasChildNodes()) {
        while (node.firstChild) {
          node.removeChild(node.firstChild );
        }
  }

};

/* ---------------------------------------------------------------- */
/*                  XUL Listbox Helper Functions                    */
/* ---------------------------------------------------------------- */

/**
 * @function removeListItems
 *
 * @memberOf OAA_CI
 *
 * @desc Removes all listitem elements from a listbox
 *
 * @param {XULListBox}  list  - XUL lsitbox element node    
 */

OAA_CI.removeListItems = function(listbox) {

  while (listbox && listbox.lastChild && listbox.lastChild.tagName === 'listitem') {
    listbox.removeChild(listbox.lastChild);
  }

};

/**
 * @function addListItem
 *
 * @memberOf OAA_CI
 *
 * @desc Adds a listitem element to a listbox
 *
 * @param {XULListBox}  list           - XUL lsitbox element node    
 * @param {Array}       column_labels  - Array of column labels    
 */

OAA_CI.addListItem = function(listbox, column_labels) {

  if (!listbox) return;
  
  var listitem = document.createElement('listitem');
  
  for (var i = 0; i < column_labels.length; i++) {
    var listcell = document.createElement('listcell');
    listcell.setAttribute('label', column_labels[i]);
    listitem.appendChild(listcell);
  }
  
  listbox.appendChild(listitem);

};

/* ---------------------------------------------------------------- */
/*                  XUL Tree Helper Functions                       */
/* ---------------------------------------------------------------- */


/**
 * @function addTreeItem
 *
 * @memberOf OAA_CI
 *
 * @desc Add a cache element object to a row in a tree
 *
 * @param  {Object} treechidren  - DOM node whos children contain XUL treeitems
 * @param  {Object} cache_item   - object in a list of cache objects
 * @param  {Array}  properties   - array of property names to display in the row
 * @param  {String} value        - if defined sets the value property of the treecell with string
 *
 * @return
 */

OAA_CI.addTreeItem = function (treechildren, cache_item, properties, value) {

  var treeitem = document.createElement('treeitem');

  OAA_CI.addTreeRow(treeitem, cache_item, properties, value);

  treechildren.appendChild(treeitem);

};


/* ---------------------------------------------------------------- */
/*                    Tree Manipulation Functions                   */
/* ---------------------------------------------------------------- */

/**
 * @function expandTree
 *
 * @memberOf expandTree
 *
 * @desc Expand all container treeitems that are closed.
 *
 * @param {String} tree_id - ID of a XUL tree
 */

OAA_CI.expandTree = function (tree_id) {
  var tree = document.getElementById(tree_id);
  var tree_view = tree.view;

  for (var i = 0; i < tree_view.rowCount; i++) {
    if (tree_view.isContainer(i) && !tree_view.isContainerOpen(i))
      tree_view.toggleOpenState(i);
  }

};

/**
 * @function collapseTree
 *
 * @memberOf OAA_CI
 *
 * @desc Collapse all container treeitems that are open.
 *
 * @note It does not work to start at the first row because
 *       the rowCount property is based on the view, and
 *       after each row is collapsed, the count changes.
 *
 * @param {String} tree_id - ID of a XUL tree
 */

OAA_CI.collapseTree = function (tree_id) {
  var tree = document.getElementById(tree_id);
  var tree_view = tree.view;

  for (var i = (tree_view.rowCount - 1); i >= 0; i--) {
    if (tree_view.isContainer(i) && 
        tree_view.isContainerOpen(i))
      tree_view.toggleOpenState(i);
  }

};

